using UnityEngine;
using System.Collections;

public class Xwing2 : MonoBehaviour 
{
	public float turnspeed = 5.0f;
	public float speed = 5.0f;
	private float trueSpeed = 0.0f;
	private float strafeSpeed = 5.0f;
	private float roll;
	private float pitch;
	private float yaw;
	private Vector3 strafe;
	private float power;
	public GameObject shoot;
	
	// Use this for initialization
	void Start () 
	{
		
	}
	
	// Update is called once per frame
	void FixedUpdate () 
	{
		roll = Input.GetAxis("Roll");
		pitch = Input.GetAxis("Pitch");
		yaw = Input.GetAxis("Yaw");
		strafe = new Vector3(Input.GetAxis("Horizontal")*strafeSpeed*Time.deltaTime, Input.GetAxis("Vertical")*strafeSpeed*Time.deltaTime, 0);
		power = Input.GetAxis("Power");
			
		//if (trueSpeed < 1f && trueSpeed > -1f){
			trueSpeed += power;
			trueSpeed = Mathf.Clamp(trueSpeed, 0f, 4f);
		//}
		
		/*
		if (trueSpeed >= 1f){
			trueSpeed = 0.999f;	
		}
		
		if (trueSpeed <= -1){
			trueSpeed = -0.99f;	
		}
		*/
		
		if (Input.GetKey("backspace")){
			trueSpeed = 0f;
		}
		
		
		Debug.Log(trueSpeed);
		
		
		//transform.TransformDirection(0, 0, trueSpeed*speed*Time.deltaTime);	
		//rigidbody.rotation = Quaternion.AngleAxis(roll, new Vector3(0,0,-1)) * Quaternion.AngleAxis(pitch, new Vector3(1,0,0));
		rigidbody.AddRelativeTorque(pitch*turnspeed*Time.deltaTime, yaw*turnspeed*Time.deltaTime, roll*turnspeed*Time.deltaTime);
		
		//rigidbody.AddRelativeTorque(pitch*turnspeed*Time.deltaTime, yaw*turnspeed*Time.deltaTime, 0);
		//rigidbody.AddRelativeTorque(pitch*turnspeed*Time.deltaTime, 0, roll*turnspeed*Time.deltaTime);
		rigidbody.AddRelativeForce(0,0,trueSpeed*speed*Time.deltaTime);
		
		//transform.TransformDirection(new Vector3(0,0,trueSpeed*Time.deltaTime));
		//rigidbody.AddRelativeForce(strafe);
		
		
		//atira
		if (Input.GetKeyDown(KeyCode.LeftControl))
		{
			Instantiate(shoot, this.transform.position, transform.rotation);
			shoot.transform.RotateAroundLocal(Vector3.right, 180);
			
		}
	}
	
	void OnCollisionEnter(Collision other)
	{
		
	}
	
	void OnColliderEnter(Collider collider){
		
		
		
	}
	
	
	
	
}

